package com.example.questionsystem.modules.qu.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HtmlQuestionRenumberUtil {

    /**
     * 处理题目HTML，去掉原有题号，并按题型重新编号
     * @param html 输入的原始HTML
     * @return 处理后的HTML
     */
    public static String renumberQuestions(String html) {
        Document doc = Jsoup.parse(html);

        Elements paragraphs = doc.select("p");
        int questionNumber = 0;

        for (Element p : paragraphs) {
            String text = p.text().trim();

            // 跳过空行
            if (text.isEmpty()) continue;

            // 去掉所有【...】标识（比如【单选题】【题干】【题目结束】等）
            text = text.replaceAll("【.*?】", "").trim();
            // 判断是否是题型标题，比如：一、对错题 二、单选题
            if (text.matches("^[一二三四五六七八九十]*[、.]?\\s*(判断题|对错题|单项选择题|单选题|多项选择题|多选题|不定项选择题|不定项|填空题|计算题|简答题|表格题|综合题|写作题|作文题)\\b.*")) {
                questionNumber = 0; // 新题型重置计数
                continue;
            }

            // 判断是否是题目行（可以是任意前缀加题号：如【题干】42．）
            if (text.matches(".*\\d+．.*")) {
                questionNumber++;

                // 使用正则去掉前缀和题号（如【题干】42．）
                String newText = text.replaceFirst("^.*?(\\d+)．", "");

                // 替换原有题号，保持原结构
                p.html("<span>" + questionNumber + "．</span>" + newText);
            }else {
                Element text1 = p.text(text);
                System.out.println(text1.html());
            }
        }

        return doc.body().html();
    }

    // 测试用 main 方法
    public static void main(String[] args) {
        String html = "<tbody><tr class=\"firstRow\" style=\"position: relative;\"><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;>序号</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\">序号</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;>项目编码</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\">项目编码</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;>项目名称</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\">项目名称</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;>项目特征描述</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\">项目特征描述</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;>计量单位</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\">计量单位</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;>工程量</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\">工程量</span></p></td><div class=\"table-row-resize-handle\" style=\"position: absolute; bottom: -2px; left: 0px; right: 0px; height: 4px; cursor: row-resize; background: transparent; z-index: 1000;\"></div></tr><tr style=\"position: relative;\"><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';&quot;>1</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';\">1</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';&quot;>011405001001</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';\">011405001001</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;>钢柱刷油漆</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\">钢柱刷油漆</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';&quot;>1.</span><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;><span class=&quot;&quot; style=&quot;white-space:pre-wrap;&quot;>构件名称： </span></span></p><p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';&quot;>2.</span><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;><span class=&quot;&quot; style=&quot;white-space:pre-wrap;&quot;>清理及刷防护材料种类： </span></span></p><p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';&quot;>3.</span><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;&quot;><span class=&quot;&quot; style=&quot;white-space:pre-wrap;&quot;>油漆品种、刷漆遍数： </span></span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';\">1.</span><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\"><span class=\"\" style=\"white-space:pre-wrap;\">构件名称： </span></span></p><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';\">2.</span><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\"><span class=\"\" style=\"white-space:pre-wrap;\">清理及刷防护材料种类： </span></span></p><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';\">3.</span><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;\"><span class=\"\" style=\"white-space:pre-wrap;\">油漆品种、刷漆遍数： </span></span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(245, 245, 245) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: center;&quot;><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';&quot;>m</span><span class=&quot;6 &quot; style=&quot;color: #000000;font-size: 12.0pt;vertical-align: super;font-size: smaller;;font-family: 'Times New Roman';&quot;>2</span></p>\" data-locked=\"true\" contenteditable=\"false\" class=\"locked-cell\" data-ue-custom=\"locked\" title=\"此单元格不可编辑\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: center;\"><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;;font-family: 'Times New Roman';\">m</span><span class=\"6 \" style=\"color: #000000;font-size: 12.0pt;vertical-align: super;font-size: smaller;;font-family: 'Times New Roman';\">2</span></p></td><td style=\"border: 2px solid rgb(0, 0, 0) !important; padding: 0mm 1.91mm; vertical-align: top; background-color: rgb(230, 243, 255) !important;\" data-original-content=\"<p class=&quot;1-5-BR DocDefaults &quot; style=&quot;text-align: left;&quot;>&amp;nbsp;</p>\" contenteditable=\"true\" title=\"此单元格可以编辑\" class=\"editable-cell\"><p class=\"1-5-BR DocDefaults \" style=\"text-align: left;\">&nbsp;</p></td><div class=\"table-row-resize-handle\" style=\"position: absolute; bottom: -2px; left: 0px; right: 0px; height: 4px; cursor: row-resize; background: transparent; z-index: 1000;\"></div></tr></tbody>";
        Document doc = Jsoup.parse(html);
        String result = renumberQuestions(html);
        System.out.println(result);
    }
}
